//
//  HYweiboCell.m
//  HY-11weibo
//
//  Created by iOSzhb on 15/8/3.
//  Copyright (c) 2015年 恒昌利通. All rights reserved.
//

#import "HYweiboCell.h"
#import "HYWeibo.h"

@interface HYweiboCell()

@property (nonatomic, weak) UIImageView *iconView;
@property (nonatomic, weak) UILabel     *nameLabel;
@property (nonatomic, weak) UIImageView *vipView;
@property (nonatomic, weak) UILabel     *textLeble;
@property (nonatomic, weak) UIImageView *pictureView;


@end

@implementation HYweiboCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIImageView *iconView    = [[UIImageView alloc] init];
        _iconView                = iconView;
        [self.contentView addSubview:_iconView];

        UILabel *nameLabel       = [[UILabel alloc] init];
        _nameLabel               = nameLabel;
        [self.contentView addSubview:_nameLabel];

        UIImageView *vipView     = [[UIImageView alloc] init];
        _vipView                 = vipView;
        [self.contentView addSubview:_vipView];

        UILabel *textLeble       = [[UILabel alloc] init];
        _textLeble               = textLeble;
        [self.contentView addSubview:_textLeble];

        UIImageView *pictureView = [[UIImageView alloc] init];
        _pictureView             = pictureView;
        [self.contentView addSubview:_pictureView];
    }
    return self;
}

- (void)setWeibo:(HYWeibo *)weibo
{
    _weibo             = weibo;
    // 1.设置数据
    [self setUpCellData:weibo];
    // 2.设置frame
    [self setUpCellFrame:weibo];
}

- (void)setUpCellData:(HYWeibo *)weibo
{
    _nameLabel.text    = weibo.name;
    _vipView.image     = (weibo.vip) ? [UIImage imageNamed:@"vip"] : nil;
    _textLeble.text    = weibo.text;
    _pictureView.image = (weibo.picture) ? [UIImage imageNamed:weibo.picture] : nil;
    _iconView.image    = [UIImage imageNamed:weibo.icon];
    
}

#define KMargin    15
#define KIconWH    30
#define KVipWH     14
#define KPictureWH 100
- (void)setUpCellFrame:(HYWeibo *)weibo
{
    self.iconView.frame = CGRectMake(KMargin, KMargin, KIconWH, KIconWH);
    
    NSDictionary *attr = @{NSFontAttributeName : [UIFont systemFontOfSize:15]};
    CGRect nRect = [_nameLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil];
    CGFloat nW = nRect.size.width;
    CGFloat nH = nRect.size.height;
    CGFloat nY = KMargin + (CGRectGetHeight(_iconView.frame) - nH) * 0.5;
    self.nameLabel.frame = CGRectMake(CGRectGetMaxX(_iconView.frame) + KMargin, nY, nW, nH);
    
    self.vipView.frame = CGRectMake(CGRectGetMaxX(_nameLabel.frame) + KMargin, nY, KVipWH, KVipWH);
    
    CGRect tRect = [_textLeble.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 25, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil];
    
    self.textLabel.frame = CGRectMake(KMargin, CGRectGetMaxY(_iconView.frame) + KMargin, tRect.size.width, tRect.size.height);
    
    
}
@end


























